package com.hyacinth.forumbite.controller;

import com.hyacinth.forumbite.common.AppConfig;
import com.hyacinth.forumbite.common.AppResult;
import com.hyacinth.forumbite.common.ResultCode;
import com.hyacinth.forumbite.exception.ApplicationException;
import com.hyacinth.forumbite.model.Article;
import com.hyacinth.forumbite.model.ArticleReply;
import com.hyacinth.forumbite.model.User;
import com.hyacinth.forumbite.service.ArticleReplyService;
import com.hyacinth.forumbite.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: Yun
 * @Date: 2025/5/25
 */
@RestController
@RequestMapping("/reply")
@Tag(name = "帖子回复相关接口")
public class ArticleReplyController {
    @Resource
    private ArticleReplyService articleReplyService;
    @Resource
    private ArticleService articleService;

    @Operation(summary = "根据帖子id回复帖子")
    @Parameters({
            @Parameter(name = "id", description = "帖子id"),
            @Parameter(name = "content", description = "回复正文")
    })
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request, Long id, String content) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        Article article = articleService.selectByPrimaryKey(id);
        if (article == null || article.getDeleteState() == 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        if (article.getState() == 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_STATE));
        }
        ArticleReply articleReply = new ArticleReply()
                .setArticleId(id)
                .setContent(content)
                .setPostUserId(user.getId());
        articleReplyService.create(articleReply);
        return AppResult.success();
    }

    @Operation(summary = "获取帖子回复列表")
    @Parameters({
            @Parameter(name = "id", description = "帖子id")
    })
    @GetMapping("/list")
    public AppResult<List<ArticleReply>> selectArticleReplyList(Long id) {
        Article article = articleService.selectByPrimaryKey(id);
        if (article == null || article.getDeleteState() == 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        return AppResult.success(articleReplyService.selectArticleReplyList(id));
    }
}
